class BunchTime
{
public:
  static Bool_t IsPosTOK(const Double_t post);
  static Int_t GetBunchNo(const Double_t time);
  static Int_t GetVTBunchNo(const Double_t time);

private:
  static const Double_t fBunchs[8];
};

const Double_t BunchTime::fBunchs[8]={2750.2, 3332.0, 3914.7, 4497.0, 5078.4, 5659.7, 6243.4, 6824.2 };

Bool_t BunchTime::IsPosTOK(const Double_t post)
{ 
  for(Int_t ib=0; ib<8; ib++){
    if(TMath::Abs( post - fBunchs[ib]) < 60. ){
      return kTRUE;
    }
  }
  return kFALSE;
}

Int_t BunchTime::GetBunchNo(const Double_t time)
{
  const Int_t startClockOffset = 260;
  const Int_t spillOffset      = 2320;
  const Int_t cyclePeriod      = 580; //480 integration + 100 reset												   
  const Int_t initialReset     = 100;
  
   // Integration cycles start at zero.                                                                                                                       
  const Int_t int_cycle = (int) TMath::Floor( (time - (double) startClockOffset - (double) initialReset) /(double) cyclePeriod );
  const Int_t int_cycle_first_bunch = (int) TMath::Floor (spillOffset / cyclePeriod);
  Int_t bunch = int_cycle - int_cycle_first_bunch;
  //std::cout<<"Bunch = "<<bunch<<std::endl;                                                                                                                 

  // Beam bunches are numbered 0 -> 7, or -1 if not a beam bunch.                                                                                            
  if(bunch < 0 || bunch > 7){bunch = -1;}

  return bunch;  
}
    
Int_t BunchTime::GetVTBunchNo(const Double_t time)
{
  //For the vertex i.e. true times 
  const Int_t startClockOffset = 0;
  const Int_t spillOffset      = 0;
  const Int_t cyclePeriod      = 580; //480 integration + 100 reset												   
  const Int_t initialReset     = 0;
  
  // Integration cycles start at zero.                                                                                                                       
  const Int_t int_cycle = (int) TMath::Floor( (time - (double) startClockOffset - (double) initialReset) /(double) cyclePeriod );
  const Int_t int_cycle_first_bunch = (int) TMath::Floor (spillOffset / cyclePeriod);
  Int_t bunch = int_cycle - int_cycle_first_bunch;
  //std::cout<<"Bunch = "<<bunch<<std::endl;                                                                                                                 

  // Beam bunches are numbered 0 -> 7, or -1 if not a beam bunch.                                                                                            
  if(bunch < 0 || bunch > 7){bunch = -1;}

  return bunch;  
}
